package regex;

import java.util.regex.*;

public class Regex {
	public static void main(String[] args) {
		Pattern p = Pattern.compile("\\d\\w");
		Matcher m = p.matcher("ab4 56_7ab");
		System.out.println("Pattern is " + m.pattern());
		while(m.find()) {
			System.out.println(m.start() + " " + m.group());
		}
		
		System.out.println("\n");
		
		Pattern p1 = Pattern.compile("a?");
		Matcher m1 = p1.matcher("baba");
		System.out.println("Pattern is " + m1.pattern());
		while(m1.find()) {
			System.out.println(m1.start() + " " + m1.end() + " " + m1.group());
		}
		
		System.out.println("\n");
		
		Pattern p2 = Pattern.compile("a?");
		Matcher m2 = p2.matcher("aba");
		System.out.println("Pattern is " + m2.pattern());
		while(m2.find()) {
			System.out.println(m2.start() + " " + m2.end() + " " + m2.group());
		}
		
		System.out.println("\n");
		
		Pattern p3 = Pattern.compile("aba");
		Matcher m3 = p3.matcher("abababa");
		System.out.println("Pattern is " + m3.pattern());
		while(m3.find()) {
			System.out.println(m3.start() + " " + m3.end() + " " + m3.group());
		}
		
		System.out.println("\n");

		Pattern p4 = Pattern.compile("\\w");
		Matcher m4 = p4.matcher("a 1 56 _Z");
		System.out.println("Pattern is " + m4.pattern());
		while(m4.find()) {
			System.out.println(m4.start() + " " + m4.end() + " " + m4.group());
		}
		
		System.out.println("\n");
		
		Pattern p5 = Pattern.compile("[a-cA-C]");
		Matcher m5 = p5.matcher("cafeBABE");
		System.out.println("Pattern is " + m5.pattern());
		while(m5.find()) {
			System.out.println(m5.start() + " " + m5.end() + " " + m5.group());
		}
		
		System.out.println("\n");
		
		Pattern p6 = Pattern.compile("\\d+");
		Matcher m6 = p6.matcher("1 a12 234b");
		System.out.println("Pattern is " + m6.pattern());
		while(m6.find()) {
			System.out.println(m6.start() + " " + m6.end() + " " + m6.group());
		}
		
		System.out.println("\n");
		
		Pattern p7 = Pattern.compile("proj1([^,])*");
		Matcher m7 = p7.matcher("proj3.txt,proj1sched.pdf,proj1,proj2,proj1.java");
		System.out.println("Pattern is " + m7.pattern());
		while(m7.find()) {
			System.out.println(m7.start() + " " + m7.end() + " " + m7.group());
		}
		
		System.out.println("\n");
		
		Pattern p8 = Pattern.compile("\\d\\d\\d([-\\s])?\\d\\d\\d\\d");
		Matcher m8 = p8.matcher("1234567 123 4567 123-4567");
		System.out.println("Pattern is " + m8.pattern());
		while(m8.find()) {
			System.out.println(m8.start() + " " + m8.end() + " " + m8.group());
		}
		
		System.out.println("\n");
		
		Pattern p9 = Pattern.compile("a.c");
		Matcher m9 = p9.matcher("ac abc a c");
		System.out.println("Pattern is " + m9.pattern());
		while(m9.find()) {
			System.out.println(m9.start() + " " + m9.end() + " " + m9.group());
		}
		
		System.out.println("\n");
		

		Pattern p10 = Pattern.compile(".*?xx");	//reluctant quantifier
		Matcher m10 = p10.matcher("yyxxxyxx");
		System.out.println("Pattern is " + m10.pattern());
		while(m10.find()) {
			System.out.println(m10.start() + " " + m10.end() + " " + m10.group());
		}
		
		System.out.println("\n");
		
		Pattern p11 = Pattern.compile(".*xx");	//greedy quantifier
		Matcher m11 = p11.matcher("yyxxxyxx");
		System.out.println("Pattern is " + m11.pattern());
		while(m11.find()) {
			System.out.println(m11.start() + " " + m11.end() + " " + m11.group());
		}
		
		System.out.println("\n");
		
	}
}
